define(function(require,exports,module){
    'use strict';

	var React = require('react');
	var $ = require('jquery');
	var XDate = require('xdate');
    var moment = require('moment');
	var xdate = new XDate();
	var jPages = require('jPages');
    var fancybox = require('fancybox');
	
	var publicObj = require('lib/public');

    //Pager
    var Pager = require('components/partial/Pager');
	
	module.exports = React.createClass({displayName: "exports",
        listData:function(p){
            var token = publicObj.get_token();

            var t_now_date = Math.round(new XDate(new XDate().toString("yyyy-MM-dd") +" 00:00:00").getTime() / 1000);
            var t_tomorrow_date = Math.round(new XDate(new XDate().addDays(1).toString("yyyy-MM-dd")+" 00:00:00").getTime() / 1000);

            p = parseInt(p);
            var page = p - 1;
            page = page > 0 ? page : 0;
            var pagesize = 10;
            var limit = " limit "+(page * pagesize)+","+pagesize+" ";


            var data = new Object();
            data.sql = "select headimg,ctime as date,email,gender,mobilephone as mobile,nick,username,referrer from qeeniao.user where (ctime >= " + t_now_date + " and ctime < " + t_tomorrow_date + ") order by ctime DESC "+limit;
            data.access_token = token;

            $.ajax({
                url:this.props.url,
                data:data,
                type:'post',
                dataType:'json',
                success:function(o){
                    if(this.isMounted()){
                        this.setState({'data':o});

                        if(o.length < pagesize){
                            this.setState({'next_stop':true});
                        }else{
                            this.setState({'next_stop':false});
                        }

                        if((p-1) <= 0){
                            this.setState({'pre_stop':true});
                        }else{
                            this.setState({'pre_stop':false});
                        }

                        this.setState({page:p});
                    }

                }.bind(this),
                error:function(xhr,status,err){
                    console.error('获取数据失败!',status,err.toString());
                }.bind(this)
            })
        },
		getInitialState:function(){
			return {
				data:[],
				data_sum:0,
                page:1,
                pages:0,
                pre_stop:true,
                next_stop:false
			};

		},
        loadRegisterDataSum:function(){
            var token = publicObj.get_token();
            var t_now_date = Math.round(new XDate(new XDate().toString("yyyy-MM-dd") +" 00:00:00").getTime() / 1000);
            var t_tomorrow_date = Math.round(new XDate(new XDate().addDays(1).toString("yyyy-MM-dd")+" 00:00:00").getTime() / 1000);
            var data = new Object();
            data.sql = "select count(*) as sum from qeeniao.user where (ctime >= " + t_now_date + " and ctime < " + t_tomorrow_date + ")";
            data.access_token = token;

            $.ajax({
                url:this.props.url,
                data:data,
                type:'post',
                dataType:'json',
                success:function(o){
                    if(this.isMounted()) {
                        this.setState({'data_sum': o[0].sum});
                    }

                }.bind(this),
                error:function(xhr,status,err){
                    console.error('获取数据失败!',status,err.toString());
                }.bind(this)
            })
        },
		componentDidMount:function(){
            this.loadRegisterDataSum();
            this.listData(1);
            $('.fancybox').fancybox({
                image    : '<img class="fancybox-image" src="{href}" alt="" />'
            });
        },
		componentDidUpdate:function(){

        },
		render:function(){
			var data_list = this.state.data.map(function(k){
                var headimg;
                var date = moment(parseInt(k.date) * 1000).format('YYYY-MM-DD HH:mm:ss');
                
                if(k.headimg){
                    headimg = "http://7u2r0u.com1.z0.glb.clouddn.com/" + k.headimg;
                }else{
                    headimg = "images/default.png";
                }

                var imgLoadError = function(e){
                    e.target.src = 'images/default.png';
                }

                var username = k.nick ? k.nick : k.username;

				return (
					React.createElement("tr", null, 
                        React.createElement("td", null, 
                            React.createElement("a", {"data-fancybox-type": "image", href: headimg, className: "fancybox"}, 
                                React.createElement("img", {src: headimg, onError: imgLoadError, className: "img-circle", style: {'width':'32px','height':'32px'}})
                            )
                        ), 
                        React.createElement("td", null, username), 
						React.createElement("td", null, k.email), 
						React.createElement("td", null, date)
					)
                );
			});

            var pager_props = {
                page : this.state.page,
                pre_stop:this.state.pre_stop,
                next_stop:this.state.next_stop,
                listData : this.listData
            };

			return (
				React.createElement("div", {className: "panel panel-default"}, 
					React.createElement("div", {className: "panel-heading"}, 
						React.createElement("h3", {className: "panel-title"}, this.props.title, " :", React.createElement("span", {className: "red"}, this.state.data_sum, "个"))
					), 
					React.createElement("div", {className: "panel-body"}, 
						React.createElement("table", {className: "table table-striped table-bordered table-hover"}, 
							React.createElement("thead", null, 
								React.createElement("tr", null, 
                                    React.createElement("th", null, "头像"), 
									React.createElement("th", null, "用户名"), 
									React.createElement("th", null, "邮箱"), 
									React.createElement("th", null, "注册时间")
								)
							), 
							React.createElement("tbody", {id: "itemContainer"}, 
								data_list
							)
						)
					), 
					React.createElement("div", {className: "panel-body"}, 
						React.createElement(Pager, React.__spread({},  pager_props))
					)
				)
            );
		}
	});

});